package com.diqizu.servlet.admin;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.diqizu.dao.ProDuctType;
import com.diqizu.entity.ProType;
import com.diqizu.servlet.BaseServlet;
import com.diqizu.utli.Result;

/**
 * Servlet implementation class chanpintypeServlet
 */
@WebServlet("/admin/ChanPinTypeServlet")
public class ChanPinTypeServlet extends BaseServlet {
  
    ProDuctType dao=new ProDuctType();
  public void getprotype(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    ArrayList<ProType> type = dao.type();
    String res = Result.toClient(0, "产品系列", type);
    PrintWriter out = resp.getWriter();
    out.write(res);
    out.flush();
    out.close();
  }
  
     public void insertprotype(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       String typeId = req.getParameter("typeId");
       String typeName = req.getParameter("typeName");
       String productTypeId = req.getParameter("productTypeId");
       ProType product=new ProType(0,typeName,productTypeId);
       boolean res=dao.insertprotype(product);
       PrintWriter out = resp.getWriter();
       if(res) {
         out.write(Result.toClient(0, "添加成功"));
       }else {
         out.write(Result.toClient(1, "添加失败"));
       }
        out.flush();
        out.close();
  }
  
  
  //修改
     public void updateprotype(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        
        //获取数据
        String typeName = req.getParameter("typeName");
        String typeId = req.getParameter("typeId");
        int typeId2=Integer.valueOf(typeId);
        ProType type=new ProType(typeId2,typeName,"");
        
         boolean res = dao.updateprotype(type);
         
         PrintWriter out = resp.getWriter();
         if(res) {
           out.write(Result.toClient(0, "修改成功"));
         }else {
           out.write(Result.toClient(1, "修改失败"));
         }
          out.flush();
          out.close();
          
        }
  
  
  //删除
     public void deleteprotype(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        
        //获取数据
        
        String typeId = req.getParameter("typeId");
        int typeId2=Integer.valueOf(typeId);
         boolean res = dao.deleteproduct(typeId2);
         PrintWriter out = resp.getWriter();
         if(res) {
           out.write(Result.toClient(0, "删除成功"));
         }else {
           out.write(Result.toClient(1, "删除失败"));
         }
          out.flush();
          out.close();
          
        }
  
}